<template>
  <div class='wrapper'>
    <img class="wrapper_img" src="http://www.dell-lee.com/imgs/vue3/user.png" alt="">
    <div class="wrapper_input">
      <input
        class="wrapper_input_content"
        placeholder="请输入手机号"
        type="text"
        v-model="mobile">
    </div>
    <div class="wrapper_input">
      <input
        class="wrapper_input_content"
        placeholder="请输入密码"
        type="password"
        v-model="password"
        autocomplete="new-password">
    </div>
    <div class="wrapper_login-button" @click="handleLogin">登 陆</div>
    <div class="wrapper_login-link" @click="handleRegisterClick">立即注册</div>
    <Toast v-if="show" :message="toastMessage"></Toast>
  </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { post } from '@/utils/request'
import Toast, { useToastEffect } from '@/components/Toast'

// 登陆逻辑
const useLoginEffect = (showToast) => {
  const router = useRouter()
  const data = reactive({ mobile: '', password: '' })
  const handleLogin = async () => {
    try {
      const { mobile, password } = data
      if (mobile === '' || password === '') {
        showToast('请输入账号密码')
      } else {
        const result = await post('/api/user/login', {
          username: data.mobile,
          password: data.password
        })
        if (result?.errno === 0) {
          localStorage.isLogin = true
          router.push({ name: 'Home' })
        } else {
          showToast('登陆失败')
        }
      }
    } catch (error) {
      showToast('请求失败')
    }
  }
  const { mobile, password } = toRefs(data)
  return {
    mobile,
    password,
    handleLogin
  }
}
// 注册逻辑
const useRegisterEffect = () => {
  const router = useRouter()
  const handleRegisterClick = () => {
    router.push({ name: 'Register' })
  }
  return { handleRegisterClick }
}
export default {
  name: 'Login',
  components: { Toast },
  data () {
    return {}
  },
  // 主流程逻辑.职责就是解释代码执行的一个流程
  setup () {
    const { show, toastMessage, showToast } = useToastEffect()
    const { mobile, password, handleLogin } = useLoginEffect(showToast)
    const { handleRegisterClick } = useRegisterEffect()
    return {
      mobile,
      password,
      show,
      toastMessage,
      handleLogin,
      handleRegisterClick
    }
  },
  mounted () {
  }
}
</script>
<style lang='scss' scoped>
@import '@/style/viriables.scss';
.wrapper{
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &_img{
    display: block;
    margin: 0 auto .4rem auto;
    width: .66rem;
    height: .66rem;
  }
  &_input{
    // box-sizing: border-box;
    height: .48rem;
    margin: 0  .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #f9f9f9;
    border: .01rem solid rgba(0,0,0,0.10);
    border-radius: .06rem;
    &_content{
      margin-top: .12rem;
      width: 100%;
      border: none;
      outline: none;
      background: none;
      line-height: .22rem;
      font-size: .16rem;
      color: $content-notice-fontcolor;
      &::placeholder{
        color: $content-notice-fontcolor;
      }
    }
  }
  &_login-button{
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: $btn-bgColor;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
    border-radius: .04rem;
    color:$bg-color;
    font-size: .16rem;
    text-align: center;
  }
  &_login-link{
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
